class Car{

    private _url:string = "";//url
    private _car3d:Laya.Sprite3D;//汽车

    private _xzAngle:number = 360;//xz角度
    private _yAngle:number = 40;//y角度

    private _radius:number = 4;//视角距离
    private _yIsUp:boolean = false;//是否向上

    private _downX:number;//鼠标按下的x
    private _downY:number;//鼠标按下的y
    private _downXzAngle:number;//鼠标按下的xz角度
    private _downYAngle:number;//鼠标按下的y角度

    /**
     * 设置车的皮肤
     */
    public setCarSkin(url:string):void{
        //url 相同
        if(this._url == url){
            return;
        }
        //重置
        this.reset();
        this._url = url;
        //检查是否加载完成
        if(Laya.loader.getRes(this._url) != null){
            this.onCreateComplete();
            return;
        }
        //未加载 开始加载
        Laya.loader.create([
                {url:this._url,type:Laya.Sprite3D}
            ], Laya.Handler.create(this,this.onCreateComplete)
        );
    }

    //重置
    private reset():void{
        //停止加载未加载完成的
        if(this._url != ""){
            Laya.loader.cancelLoadByUrl(this._url);
        }
        //移除上一次显示的车
        if(this._car3d != null){
            this._car3d.destroy(true);
            this._car3d.removeSelf();
        }
        //重置初始显示位置
        this._xzAngle = 360;
        this._yAngle = 40;
        this._yIsUp = false;
        //开始自动旋转
        this.stopAutoRotate();
    }

    //加载完成
    private onCreateComplete():void{
        this._car3d = Laya.loader.getRes(this._url);
        Global.scene.addChild(this._car3d);

        //开始自动旋转
        this.startAutoRotate();

        //添加侦听
        Laya.stage.on(Laya.Event.MOUSE_DOWN,this,this.mouseDownHandler);
        Laya.stage.on(Laya.Event.MOUSE_UP,this,this.mouseUpHandler);
    }

    //鼠标按下
    private mouseDownHandler(e:Laya.Event):void{
        if(UIManager.uiLayer.contains(e.target)){
            return;
        }
        //停止自动旋转
        this.stopAutoRotate();
        //保存按下的鼠标xy
        this._downX = Laya.stage.mouseX;
        this._downY = Laya.stage.mouseY;
        //角度信息
        this._downXzAngle = this._xzAngle;
        this._downYAngle = this._yAngle;
        //添加侦听
        Laya.stage.on(Laya.Event.MOUSE_MOVE,this,this.mouseMoveHandler);
    }

    //鼠标弹起
    private mouseUpHandler():void{
        //开始自动旋转
        this.startAutoRotate();
        //移除侦听
        Laya.stage.off(Laya.Event.MOUSE_MOVE,this,this.mouseMoveHandler);
    }

    //鼠标移动
    private mouseMoveHandler():void{
        var moveX:number = this._downX - Laya.stage.mouseX;
        var moveY:number = this._downY - Laya.stage.mouseY;
        //计算移动角度
        this._xzAngle = this._downXzAngle - moveX / 10;
        this._yAngle = this._downYAngle - moveY / 10;
        //刷新摄像机位置
        this.updateCameraPos();
    }

    //开始自动旋转
    private startAutoRotate():void{
        Laya.timer.clear(this,this.autoRotate);
        Laya.timer.frameLoop(1,this,this.autoRotate);
    }

    //停止自动旋转
    private stopAutoRotate():void{
        Laya.timer.clear(this,this.autoRotate);
    }

    //自动旋转
    private autoRotate():void{
        this._xzAngle -= 0.1;
        this._yAngle -= this._yIsUp ? -0.03 : 0.03;
        //刷新摄像机位置
        this.updateCameraPos();
    }

    //检测边界
    private rekonBorder():void{
        this._xzAngle = this._xzAngle <= 0 ? 360 : this._xzAngle;
        if(this._yAngle <= 10){
            this._yIsUp = true;
        }else if(this._yAngle >= 60){
            this._yIsUp = false;
        }
        this._yAngle = this._yAngle <= 10 ? 10 : this._yAngle;
        this._yAngle = this._yAngle >= 60 ? 60 : this._yAngle;
    }

    //刷新摄像机位置
    private updateCameraPos():void{
        //计算边界
        this.rekonBorder();
        //计算摄像机位置
        var xzRadian:number = Math.PI / 180 * this._xzAngle;
        var yRadian:number = Math.PI / 180 * this._yAngle;
        var x:number = Math.sin(xzRadian) * this._radius;
        var y:number = Math.sin(yRadian) * this._radius;
        var z:number = -Math.cos(xzRadian) * this._radius;
        Global.camera.transform.position = new Laya.Vector3(x,y,z);
        Global.camera.transform.lookAt(this._car3d.transform.position,new Laya.Vector3(0,1,0));
    }
}